from xml.sax.handler import ContentHandler
from xml.sax import parse
import re
import easysax


class HeadlineHandler(ContentHandler):

    in_headline = 0
    global num

    def __init__(self, headlines,num):
        ContentHandler.__init__(self)
        self.headlines = headlines
        self.data = []
        self.num = num

    def startElement(self, name, attrs):
        if name == 'title':
            self.in_headline = 1
        elif name == 'heading':
            self.in_headline = 1
        elif name == 'content':
            self.in_headline = 1

    def endElement(self, name):
        if name == 'title':
            text = ''.join(self.data)
            self.data = []
            self.headlines.append(text)
            self.in_headline = 0
        elif name == 'heading':
            text = ''.join(self.data)
            self.data = []
            self.headlines.append(text)
            self.in_headline = 0
        elif name == 'content':
            text = ''.join(self.data)
            self.data = []
            self.headlines.append(text)
            self.in_headline = 0
        self.num = self.num + 1
            
    def characters(self, string):
        if self.in_headline:
            string = string.strip()
            self.data.append(string)
        if(self.num == 2):
            print num
            ChangeHeadlines(self.headlines)
            exit
        

class ChangeHeadlines:
    
    num = 0
    
    def __init__(self, headlines):
        self.headlines = headlines
        print 'Got Here'
        change(headlines)
    
    def change(self, headlines):
        print 'The current title is: ' + headlines[0]
        cmd = raw_input('Would you like to change the title? y or n ')
        if(cmd.strip() == 'y'):
            titlecmd = raw_input('What would you like to change it to? ')
            headlines[0] = titlecmd
        
        print 'The current heading is: ' + headlines[1]
        cmd = raw_input('Would you like to change the heading? y or n ')
        if(cmd.strip() == 'y'):
            headingcmd = raw_input('What would you like to change it to? ')
            headlines[1] = headingcmd
        
        print 'The current content is: ' + headlines[2]
        cmd = raw_input('Would you like to change the content? y or n ')
        if(cmd.strip() == 'y'):
            contentcmd = raw_input('What would you like to change it to? ')
            headlines[2] = contentcmd
        
        print 'The following are your requested changes: '
        print 'Title: ' + headlines[0]
        print 'Heading: ' + headlines[1]
        print 'Content: ' + headlines[2]

    
headlines = []
num = 0
parse('index.xml', HeadlineHandler(headlines,num))
for h in headlines:
    print h
#ChangeHeadlines(headlines)
